<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\User;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   $videos=Video::take(1)->get();
        $experts=Expert::take(2)->get();
        return view('front.index')->with('experts',$experts)
            ->with('videos',$videos);
    }

    public function people()
    {
        return view('front.people');
    }
    public function list()
    {
        return view('front.people_list');
    }
    public function list_adm()
    {
        $users=User::all();
        return view('admin.users-adm')->with('users',$users);
    }
    public function login()
    {
        return view('admin.login');
    }
    public function dologin(Request $request)
    {
        $name=$request->name;
        $password=$request->password;
        if (Auth::attempt(['name'=>$name,'password'=>$password])){
            return json_encode(['success'=>true,'msg'=>"登陆成功"]);
        }
        else{
            return json_encode(['success'=>false,'msg'=>"登陆失败"]);
        }
    }
    public function logout()
    {
        Auth::logout();
        return redirect('/login');
    }





    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(){
        return view('admin.create-user');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request){
        $datas=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，-或_，不能包含其他字符',
            'name.min'=>'用户名太短，至少要有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长的不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致',
        ];
        $validate=Validator::make($datas,$rules,$message);
        if ($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $datas['password']=Hash::make($datas['password']);
        User::create($datas);
        return json_encode(['success'=>true,'msg'=>'success']);
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */



    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id){
        $user=User::find($id);
        if ($user==NULL){
            abort(404);
        }
        return view('admin.password')->with('user',$user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update($id,Request $request){
        $user=User::find($id);
        if ($user==NULL){
            abort(404);
        }
        $datas=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长的不能少于:min个字符',
            'password.confirmed'=>'两次密码输入不一致',
        ];
        $validate=Validator::make($datas,$rules,$message);
        if ($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user->password=Hash::make($datas['password']);
        $user->save();
        return json_encode(['success'=>true,'msg'=>'success']);
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $user=User::find($id);
        $user->delete();
        return redirect('/adm/user');
    }
}
